package com.mchsdk.paysdk.http.process;

import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.lidroid.xutils.http.RequestParams;
import com.mchsdk.paysdk.bean.ChannelAndGameInfo;
import com.mchsdk.paysdk.bean.PersonalCenterModel;
import com.mchsdk.paysdk.config.MCHConstant;
import com.mchsdk.paysdk.http.request.UploadRoleRequest;
import com.mchsdk.paysdk.utils.MCLog;
import com.mchsdk.paysdk.utils.RequestParamUtil;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by smile on 2017/8/18.
 */

public class UploadRoleProcess {

    private static final String TAG = "UploadRoleProcess";
    //区服名称
    String serverName;
    //游戏内昵称
    String roleName;
    //等级
    String roleLevel;
    //游戏id
    String gameId;
    //服务器id
    String serverId;


    public void setServerName(String serverName) {
        this.serverName = serverName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public void setRoleLevel(String roleLevel) {
        this.roleLevel = roleLevel;
    }

    public void setGameId(String gameId) {
        this.gameId = gameId;
    }

    public void setServerId(String serverId) {
        this.serverId = serverId;
    }

    public void post(Handler handler) {
        if (null == handler) {
            MCLog.e(TAG, "fun#post handler is null");
            return;
        }
        Map<String, String> map = new HashMap<String, String>();
        //	map.put("sdk_version", "0");//表示android发送的请求，固定值0
        map.put("user_id", PersonalCenterModel.getInstance().getUserId());
        map.put("game_id", ChannelAndGameInfo.getInstance().getGameId());
        map.put("server_id", this.serverId);
        map.put("server_name", this.serverName);
        map.put("game_player_name", this.roleName);
        map.put("promote_id", ChannelAndGameInfo.getInstance().getChannelId());
        map.put("role_level", this.roleLevel);
        map.put("sdk_version", "1");
        for (Map.Entry entry : map.entrySet()) {
            Log.e("->>",entry.getKey()+"\t\t"+entry.getValue()+"\n");
        }
        String param = RequestParamUtil.getRequestParamString(map);
        if (TextUtils.isEmpty(param)) {
            MCLog.e(TAG, "fun#post param is null");
            return;
        }
        RequestParams params = new RequestParams();
        MCLog.w(TAG, "fun#post postSign:" + map.toString());
        try {
            params.setBodyEntity(new StringEntity(param.toString()));
        } catch (UnsupportedEncodingException e) {
            params = null;
            MCLog.e(TAG, "fun#post UnsupportedEncodingException:" + e);
        }

        if (null != params) {
            UploadRoleRequest uploadRoleRequest = new UploadRoleRequest(handler, null);
            uploadRoleRequest.post(MCHConstant.getInstance().getUploadRoleUrl(), params);
        } else {
            MCLog.e(TAG, "fun#post RequestParams is null");
        }

    }
}
